package com.cyou.qb.front.web;

import java.text.ParseException;
import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cyou.nad.dao.PojoPage;
import com.cyou.nad.tag.table.pagebar.PageBar173App;
import com.cyou.qb.common.domain.Active;
import com.cyou.qb.common.domain.FrontActivity;
import com.cyou.qb.common.service.ActivityService;
import com.cyou.qb.common.service.AwardService;
import com.cyou.qb.common.service.UserDrawTraceService;

/**
 * <p>ActivityController</p>
 * <p>Description:处理活动列表信息。查询活动列表信息Active-list.jsp</p>
 * 
 * @author wangwenlong
 * @version
 */
@Controller
@RequestMapping(value = "/activity")
public class ActivityController extends BaseController{
	@Resource(name="ActivityServiceCacheImpl")
	private ActivityService activityService;

	@Resource(name="UserDrawTraceServiceCacheImpl")
	private UserDrawTraceService userDrawTraceService;
	//findActiveDrawTimes
	@Resource(name="AwardServiceCacheImpl")
	private AwardService awardService;
	
	/**
	 * 获取活动列表
	 * @throws ParseException 
	 */
	@RequestMapping(value = "/index")
	public String index(Model model,HttpServletRequest request, HttpServletResponse response) throws ParseException{
		String resultRequest = request.getParameter("pageOrder");
		if(resultRequest == null){
			resultRequest = "pageActive";
		}
		PojoPage<Active> page = activityService.pageQuery(resultRequest, getPageNo(), getPageSize(5));

		for(int i=0;i<page.getData().size();i++){
			Active active = page.getData().get(i);
			int drawNum = userDrawTraceService.findActiveDrawTimes(active.getId()).intValue();
			int count = awardService.findActiveMaxAwardNum(active.getId());
			FrontActivity frontActivity = new FrontActivity();
			try{
				BeanUtils.copyProperties(active, frontActivity);
			}catch(BeansException e){
				e.printStackTrace();
			}
			frontActivity.setAwardCount(count);
			frontActivity.setDrawNum(drawNum);
			frontActivity.setEndFlag(active.getEndTime().getTime() - new Date().getTime());
			request.setAttribute("pageOrder", resultRequest);
			page.getData().set(i, frontActivity);
		}
		model.addAttribute("page", page);
		model.addAttribute("bar",PageBar173App.getInstance());
		return "activity/activitylist";
		
	}
}
